//
//  ZhangDanTableViewController.m
//  ErJie
//
//  Created by yaorenjin on 2017/1/26.
//  Copyright © 2017年 yaorenjin. All rights reserved.
//

#import "ZhangDanTableViewController.h"
#import "CoreDataManager.h" 
#import "ZhangDanListTableViewCell.h"
#import "ZhangDanHeaderView.h"  
#import "ZhangDanShangPinRelationEntity+CoreDataClass.h"

@interface ZhangDanTableViewController ()
@property(nonatomic,strong)NSMutableArray* dataList;
@property(nonatomic,strong)ZhangDanHeaderView* headerView;
@end

@implementation ZhangDanTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"ZhangDanHeaderView" owner:nil options:nil] lastObject];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (IBAction)addItem:(id)sender {
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.from == nil) {
        
        return nil;
    }

    CGFloat lirun = 0;
    CGFloat sale = 0;
    CGFloat chengBen = 0;
    CGFloat weixin = 0;
    CGFloat qiankuan = 0;
    CGFloat dakuan =0;
    CGFloat xianjin = 0;
    NSInteger shuLiang = 0;
    
    NSMutableArray* zdsprIdsArry = [NSMutableArray array];
    
    for (ZhangDanEntity* entity in self.dataList) {
        
        lirun += [entity.lirun integerValue];
        sale += [entity.zongjia integerValue];
        chengBen += [entity.chenben integerValue];
    
        [zdsprIdsArry addObject:entity.zdsprIds];
        //微转
        if ([entity.payType integerValue] == 0) {
            
            weixin  += [entity.zongjia integerValue];
        }
        //打款
        else if ([entity.payType integerValue] == 1)
        {
            dakuan += [entity.zongjia integerValue];
            
        }
        //现金
        else if ([entity.payType integerValue] == 2)
        {
            xianjin += [entity.zongjia integerValue];
            
        }
        //欠款
        else if ([entity.payType integerValue] == 3)
        {
            qiankuan += [entity.zongjia integerValue];
            
        }
    }
    
    self.headerView.lirunLabel.text = [NSString stringWithFormat:@"%0.2lf",lirun];
    self.headerView.saleLabel.text = [NSString stringWithFormat:@"%0.2lf",sale];
    self.headerView.chengBenLabel.text = [NSString stringWithFormat:@"%0.2lf",chengBen];
    self.headerView.weinXinLabel.text = [NSString stringWithFormat:@"%0.2lf",weixin];
    self.headerView.qianKuanLabel.text = [NSString stringWithFormat:@"%0.2lf",qiankuan];
    self.headerView.dakuanLabel.text = [NSString stringWithFormat:@"%0.2lf",dakuan];
    self.headerView.xianJinLabel.text = [NSString stringWithFormat:@"%0.2lf",xianjin];
    NSMutableArray* glEntityArry = [[CoreDataManager shareManager] selectZhangDanShangPinRelationWithIds:[zdsprIdsArry componentsJoinedByString:@","]];
    for (ZhangDanShangPinRelationEntity* entity in glEntityArry) {
        
        shuLiang += entity.shuliang;
    }
    self.headerView.shuLiangLabel.text = [NSString stringWithFormat:@"%ld",shuLiang];
    
    return self.headerView;
}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.from == nil) {
        
        return 0.f;
    }
    
    return 80;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZhangDanListTableViewCell"];
    
    if ([cell respondsToSelector:@selector(reloadData:)]) {
        
        [cell performSelector:@selector(reloadData:) withObject:[self.dataList objectAtIndex:indexPath.row]];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [[ZhangDanListTableViewCell shangPinInfoWithEntity:[self.dataList objectAtIndex:indexPath.row]] componentsSeparatedByString:@"\n"].count * 20 + 100;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    
    ZhangDanEntity* zdEntity = [self.dataList objectAtIndex:indexPath.row];
    
    if (self.from == nil) {
        
        return YES;
    }
    else if ([self.from integerValue] == 4 && [zdEntity.isPay integerValue] == 0)
    {
        return YES;
    }
    
    return NO;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        
        if (self.from == nil) {

            if ([[CoreDataManager shareManager] deleteZhangDan:[self.dataList objectAtIndex:indexPath.row]]) {
                
                [self.dataList removeObjectAtIndex:indexPath.row];
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            };
        }
        else
        {
            ZhangDanEntity* zdEntity = [self.dataList objectAtIndex:indexPath.row];

            [[CoreDataManager shareManager] updateZhangDanIsPayWithZid:zdEntity.zId];
            [self.dataList removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.from == nil) {
        
        return @"删除";
    }
    else
    {
        return @"已还欠款";
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.from == nil) {
        
        self.dataList = [[CoreDataManager shareManager] selectZhangDan];

    }
    else if ([self.from integerValue] == 0)
    {
        self.navigationItem.rightBarButtonItem = nil;
        
        self.dataList = [[CoreDataManager shareManager] selectZhangDanWithToday];

    }
    else if ([self.from integerValue] == 1)
    {
        self.navigationItem.rightBarButtonItem = nil;

        self.dataList = [[CoreDataManager shareManager] selectZhangDanWithWeek];
    }
    else if ([self.from integerValue] == 2)
    {
        self.navigationItem.rightBarButtonItem = nil;

        self.dataList = [[CoreDataManager shareManager] selectZhangDanWithMonth];
    }
    else if ([self.from integerValue] == 3)
    {
        self.navigationItem.rightBarButtonItem = nil;

        self.dataList = [[CoreDataManager shareManager] selectZhangDanWithYear];
    }
    else if ([self.from integerValue] == 4)
    {
        self.navigationItem.rightBarButtonItem = nil;
        
        self.dataList = [[CoreDataManager shareManager] selectQianKuanZhangDan];
    }

    [self.tableView reloadData];
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
